package com.tanker.common.utils.microRental;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tanker.common.utils.HttpClientUtil;

public class MicroRentalHttpUtil {

	private static Logger logger = LoggerFactory.getLogger(MicroRentalHttpUtil.class);

	public static void appPost(Map<String, Object> reqData, String url) throws Exception {
		String key=getKey(reqData,url);
		String entyKey= MicroRentalEncrypt.loadPublicKeyByFile(null);
		logger.info("获取私钥key={}",entyKey);
    	String sign=MicroRentalEncrypt.getSignature(key,entyKey);
    	reqData.put("sign", sign);
        String result=	HttpClientUtil.doPostNew(MicroRentalConfUtil.BASE_URL+url,reqData);
		logger.info("得到元数据key={},result={}",key,result);
		
	}
	//对URI进行编码得到 %2Fcar%2F+position%2Fdetail
	//将除“sign”外的所有参数按key进行字典升序排列，然后进行URL编码得到 
	private static String getKey(Map<String, Object> reqData, String url) throws Exception {
			String key1=URLEncoder.encode(url, "UTF-8");
			
			
			 StringBuffer sbff = new StringBuffer();
		    	//获取车型信息
				TreeMap<String, Object> treeMap = new TreeMap<String, Object>();
		    	treeMap.putAll(reqData);
		    	for(Map.Entry<String, Object> m: treeMap.entrySet()){
		    		sbff.append(m.getKey()).append("=").append(m.getValue()).append("&");
		    	}
		    	String key2=sbff.toString();
		    	key2=key2.substring(0, key2.length()-1);
		    	key2=URLEncoder.encode(key2, "UTF-8");
//		    	System.out.println(key2);
//		    	System.out.println(URLEncoder.encode(key2, "UTF-8"));
		return key1+key2;
	}
	//%2Fcar%2Fposition%2Fdetail
	//%2Fcar%2F+position%2Fdetail
	//lpNum%E4%BA%ACQWB1121uid2567892221

	//lpNum%3D%E4%BA%ACQWB1121%26uid%3D2567892221
	//lpNum%3D%E4%BA%ACQWB1121%26uid%3D2567892221
	//6R77FY+rl3IMf0wHlO+B156iVzc=
	public static void main(String[] args) throws Exception {
		String key1=URLEncoder.encode("/car/ position/detail", "UTF-8");

		Map<String, Object> reqData=new HashMap<String, Object>();
		//uid=2567892221&lpNum=京QWB1121
		reqData.put("uid", "2567892221");
		reqData.put("lpNum", "京QWB1121");
		 StringBuffer sbff = new StringBuffer();
	    	//获取车型信息
			TreeMap<String, Object> treeMap = new TreeMap<String, Object>();
	    	treeMap.putAll(reqData);
	    	for(Map.Entry<String, Object> m: treeMap.entrySet()){
	    		sbff.append(m.getKey()).append("=").append(m.getValue()).append("&");
	    	}
	    	String key2=sbff.toString();
	    	key2=key2.substring(0, key2.length()-1);
	    	key2=URLEncoder.encode(key2, "UTF-8");
	    	System.out.println(key2);
	    	System.out.println(key1);
	    	System.out.println(URLEncoder.encode(key2, "UTF-8"));
	    	String n=MicroRentalEncrypt.getSignature(key1+ key2,"ddddddddddddddddddddddddddddddddddddd");
	    	System.out.println(n);
	}
}

